package demo2;

public class MyCircularQueue2 {
    public int[] elem;
    public int front;//队头下标
    public int rear;//队尾下标
    public boolean flag;
    public MyCircularQueue2(int k) {
        this.elem = new int[k];
    }

    //入列
    public boolean enQueue(int value) {
        //判断有没有满
        if (isFull()) return false;
        this.elem[rear] = value;
        this.rear = (rear + 1) % elem.length;
        flag = true;
        return true;
    }

    //出列
    public boolean deQueue() {
        if (isEmpty()) return false;
        this.front = (this.front + 1) % this.elem.length;
        flag = false;
        return true;
    }

    //获得队头元素
    public int Front() {
        if (isEmpty()) return -1;
        return this.elem[this.front];
    }

    //获得队尾元素
    public int Rear() {
        if (isEmpty()) return -1;
        if (rear == 0) {
            return this.elem[elem.length-1];
        } else {
            return this.elem[rear-1];
        }
    }

    public boolean isEmpty() {
        if (rear == front && !flag ) {
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if (rear == front && flag) {
            return true;
        }
        return false;
    }
}
